using System;
using System.Xml.Serialization;

using ClientTools.Library.Interfaces;

namespace ClientTools.Client.Library.Settings
{
    public class Profile
    {
        [XmlAttribute("ProfileName")]
        public string ProfileName { get; set; }

        public string UmbracoUserName { get; set; }

        public string UmbracoPassword { get; set; }

        [XmlElement("DBConnectionString")]
        public string DbConnectionString { get; set; }

        public string ServiceUrl { get; set; }

        //private string _packagerServiceUrl;

        //public string PackagerServiceUrl
        //{
        //    get { return _packagerServiceUrl; }
        //    set { _packagerServiceUrl = value; }
        //}

        private string providerName;
        public string ProviderName
        {
            get { return providerName; }
            set { providerName = value; providerObject = null; }
        }

        public void InitProvider() 
        {
            var pi = Provider.ProviderManager.ProviderInfos.GetProviderByName(providerName);
            if (pi != null)
                providerObject = (IProvider)Activator.CreateInstance(pi.ProviderType);
            switch (providerObject.GetType().Name)
            {
                case "WebserviceProvider":
                    //providerObject = new Library.Provider.WebserviceProvider(this.ServiceFolderUrl, this.UmbracoUserName, this.UmbracoPassword);

                    var wsp = (Provider.WebserviceProvider) providerObject;
                    wsp.ServiceUrl = ServiceUrl;
                    wsp.UserName = UmbracoUserName;
                    wsp.Password = UmbracoPassword;
                    break;
                case "DatabaseProvider":
                    //Library.Provider.DatabaseProvider dbp = (Library.Provider.DatabaseProvider) providerObject;
                    //dbp.ConnectionString = this.DbConnectionString;
                    break;
            }
        }

        private IProvider providerObject;
        [XmlIgnore] 
        public IProvider ProviderObject 
        {
            get {
                if (providerObject == null)
                {
                    var pi = Provider.ProviderManager.ProviderInfos.GetProviderByName(providerName);
                    if (pi != null)
                        providerObject = (IProvider)Activator.CreateInstance(pi.ProviderType);
                }
                return providerObject; 
            }
            set { 
                providerObject = value;
                providerName = providerObject.ProviderName;
            }
        }
    }
}
